import './admin_home.html';

import '../../components/static_knob/static_knob.js';

import { Template } from 'meteor/templating';
import Chart from 'chart.js';
import { Shops } from '/imports/api/shops/shops.js';

Template.App_admin_home.onCreated(function(){
  let self = this;
  self.shop = new ReactiveVar({
    name: "载入中",
  })
  self.autorun(function(c){
    if (Meteor.userId() != null) {
      Meteor.call("users.getone", Meteor.userId(), function(error, result){
        if (error) {
          console.log(error);
          return false;
        }

        let shop = Shops.findOne({_id: result.shop_belongs});
        console.log(shop);
        self.shop.set(shop);
      });

    }else{
      FlowRouter.go('/login');
      Message.sendError("请先登录", '');
    }

  });

});


Template.App_admin_home.onRendered(function(){
  var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ["1号","3号", "5号", "7号", "10号", "15号", "20号", "25号", "30号"],
        yLables: "%百分比",
        datasets: [{
            label: '本月平均胜任力',

            data: [20, 22, 45, 87, 23, 97,12,2,55],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});
});

Template.App_admin_home.helpers({
  knob_render(title, subtitle, fgColor, bgColor,value){
    return {
      title,
      subtitle,
      fgColor,
      bgColor,
      value,
    };
  },
  employee_title(){
    return "187名";
  },

  employee_subtitle(){
    return "本店员工数量";
  },
  employee_fgColor(){
    return "#2B99E6";
  },
  employee_bgColor(){
    return "#303030";
  },
  employee_value(){
    return 187;
  },
  shop_name(){
    return Template.instance().shop.get().name;
    // return '';
  },
  shop_id(){
    return Template.instance().shop.get()._id;
  },

});
